/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.actor;

import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import com.cxhd.pacs.DataConfig;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class StartDicomExeActor extends UntypedActor {

    @Override
    public void onReceive(Object o) throws Exception {
        Record info = (Record) o;
        System.out.println("info"+info);
        if (!testPacs("127.0.0.1", Integer.parseInt(info.get("port")+""))) {
            runExe(info);
        }

    }

    public void runExe(Record info) {
        try {
            Runtime r = Runtime.getRuntime();
            System.out.println("cmd.exe /c "+DataConfig.getInstance().getConfig().get("dicomServerPath")+" "+info.get("file_path")+info.getStr("study_iuid")+".xml");
            Process p = r.exec("cmd.exe /c "+DataConfig.getInstance().getConfig().get("dicomServerPath")+" "+info.get("file_path")+info.getStr("study_iuid")+".xml");

        } catch (Exception ex) {

        }
    }

    public static boolean testPacs(String ip, int port) {
        Socket s = null;
        try {
            s = new Socket(ip, port);
            if (s.isConnected()) {
                return true;
            } else {
                return false;
            }
        } catch (UnknownHostException ex) {
            // ex.printStackTrace();
            System.out.println("cannot connect to " + ip + ":" + port);
            return false;
        } catch (IOException ex) {
            // ex.printStackTrace();
            System.out.println("cannot connect to " + ip + ":" + port);
            return false;
        } finally {
            if (s != null) {
                try {
                    s.close();
                } catch (IOException ex) {
                    // ex.printStackTrace();
                }
            }
        }
    }

}
